﻿using LandManager.Web.Models;
using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Web;

namespace LandManager.Web.Service
{
    public class UserService : IService<UserInfo>
    {

        public UserInfo FindUserWithNameAndPwd(string name, string password)
        {
            using (DataComDataContext db = new DataComDataContext())
            {
                DataLoadOptions options = new DataLoadOptions();
                options.LoadWith<UserInfo>(c => c.Agency);
                db.LoadOptions = options;

                var q = from c in db.UserInfo
                        where c.user_name == name && c.user_password == password
                        select c;
                if (q.Count() > 0)
                    return q.SingleOrDefault();
                else
                    return null;
            }
        }
        public bool Add(UserInfo t)
        {
            using (DataComDataContext db = new DataComDataContext())
            {
                try
                {
                    db.UserInfo.InsertOnSubmit(t);
                    db.SubmitChanges();

                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        public bool Delete(UserInfo t)
        {
            throw new NotImplementedException();
        }

        public bool Edit(UserInfo t)
        {
            using (DataComDataContext db = new DataComDataContext())
            {
                try
                {
                    var q = from c in db.UserInfo
                            where c.user_id == t.user_id
                            select c;

                    UserInfo oneUser = q.FirstOrDefault();

                    oneUser.user_name = t.user_name;
                    oneUser.user_password = t.user_password;
                    oneUser.user_isAdmin = t.user_isAdmin;
                    oneUser.user_isRemember = t.user_isRemember;

                    db.SubmitChanges();

                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }

        public List<UserInfo> GetList()
        {
            using (DataComDataContext db = new DataComDataContext())
            {
                DataLoadOptions options = new DataLoadOptions();
                options.LoadWith<UserInfo>(c => c.Agency);
                db.LoadOptions = options;

                var q = from c in db.UserInfo
                        select c;
                if (q.Count() > 0)
                    return q.ToList();
                else
                    return null;
            }
        }

        public UserInfo FindByID(int id)
        {
            using (DataComDataContext db = new DataComDataContext())
            {
                DataLoadOptions options = new DataLoadOptions();
                options.LoadWith<UserInfo>(c => c.Agency);
                db.LoadOptions = options;

                var q = from c in db.UserInfo
                        where c.user_id == id
                        select c;
                if (q.Count() > 0)
                    return q.FirstOrDefault();
                else
                    return null;
            }
        }
    }
}